﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SEO.Entity;

namespace SEO.Business
{
    /// <summary>
    /// 网编文章收录情况
    /// </summary>
    public class BisArticleSEO
    {
        /// <summary>
        /// 验证文章是否百度收录
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="strArticleUrl"></param>
        /// <returns></returns>
        public bool CheckBaiduSEO(int nID, int nSorID, int nKeywordsID, int nKeywordsID1, string strArticleUrl, DateTime dBeginTime, ref int nWebSEOID)
        {
            //验证文章是否已经收录
            BisWebSEO bisWebSEO = new BisWebSEO();
            ////去"/"
            //if (strArticleUrl.Substring(strArticleUrl.Length - 1, 1) == "/")
            //{
            //    strArticleUrl = strArticleUrl.Substring(0, strArticleUrl.Length - 1);
            //}
            DataTable dtWebSEO = bisWebSEO.GetWebSEO(nSorID, nKeywordsID, nKeywordsID1, strArticleUrl, dBeginTime);
            nWebSEOID = 0;
            if (dtWebSEO != null && dtWebSEO.Rows.Count > 0)
            {
                nWebSEOID = Convert.ToInt32(dtWebSEO.Rows[0]["ID"]);
                bool bResult = UpdateStatus(nID, nWebSEOID, true);
                return bResult;
            }
            else
            {
                //验证去掉www的域名
                if (strArticleUrl.ToLower().IndexOf("http://www.") == 0)
                {
                    strArticleUrl = strArticleUrl.Substring(0, 7) + strArticleUrl.Substring(11, strArticleUrl.Length-11);
                    dtWebSEO = bisWebSEO.GetWebSEO(nSorID, nKeywordsID, nKeywordsID1, strArticleUrl, dBeginTime);
                    if (dtWebSEO != null && dtWebSEO.Rows.Count > 0)
                    {
                        nWebSEOID = Convert.ToInt32(dtWebSEO.Rows[0]["ID"]);
                        bool bResult = UpdateStatus(nID, nWebSEOID, true);
                        return bResult;
                    }
                }
            }
            dtWebSEO.Dispose();
            return false;
        }

        /// <summary>
        /// 计算文章提成情况
        /// </summary>
        /// <param name="nID">文章ID</param>
        /// <param name="nUrlType">类型</param>
        /// <param name="nWebSEOID">百度收录文章ID</param>
        /// <param name="strTopDomain">顶级域名</param>
        /// <param name="nKeywordsID">关键字ID</param>
        /// <param name="nKeywordsID1">关键字ID</param>
        /// <param name="dBeginTime">开始时间</param>
        /// <param name="dEndTime">结束时间</param>
        /// <param name="sysConfig">配置信息</param>
        /// <returns></returns>
        public bool CountArticleMomey(int nID, int nUrlType, int nWebSEOID, string strTopDomain,string strDomain, int nKeywordsID, int nKeywordsID1, DateTime dBeginTime, DateTime dEndTime, SysConfig sysConfig)
        {
            //是否特殊提成网站
            BisDeductWeb bisInfo = new BisDeductWeb();
            DeductWeb deductWeb = bisInfo.Get(strTopDomain.ToLower());
            if (deductWeb != null)
            {
                UpdateDeduct(nID, true, deductWeb.ID.Value);
            }
            else
            {
                //验证二级域名是否特殊提成
                if (!string.IsNullOrEmpty(strDomain))
                {
                    deductWeb = bisInfo.Get(strDomain.ToLower());
                    if (deductWeb != null)
                    {
                        UpdateDeduct(nID, true, deductWeb.ID.Value);
                    }
                }
            }
            double dMomey = 0;
            //计算关键字1提成
            if (nKeywordsID > 0)
                dMomey += CountKeyMomeny(nUrlType, nWebSEOID, nKeywordsID, deductWeb, dBeginTime, dEndTime, sysConfig);
            //计算关键字2提成
            if (nKeywordsID1 > 0)
                dMomey += CountKeyMomeny(nUrlType, nWebSEOID, nKeywordsID1, deductWeb, dBeginTime, dEndTime, sysConfig);
            if (dMomey > 0)
                return UpdateIsPass(nID, true, dMomey, dEndTime);
            return true;
        }

       


        /// <summary>
        /// 计算文章关键字提成
        /// </summary>
        /// <param name="nUrlType">类型</param>
        /// <param name="nWebSEOID">百度收录文章ID</param>
        /// <param name="nKeywordsID">关键字ID</param>
        /// <param name="deductWeb">提成网站</param>
        /// <param name="dBeginTime">开始时间</param>
        /// <param name="dEndTime">结束时间</param>
        /// <param name="sysConfig">配置信息</param>
        /// <returns></returns>
        public double CountKeyMomeny(int nUrlType, int nWebSEOID, int nKeywordsID, DeductWeb deductWeb, DateTime dBeginTime, DateTime dEndTime, SysConfig sysConfig)
        {
            DataTable dtWebSEO = new BisWebSEO().GetWebSEOReportList(nWebSEOID, nKeywordsID, dBeginTime, dEndTime);
            if (dtWebSEO != null)
            {
                if (dtWebSEO.Rows.Count >= sysConfig.BaiduDay.Value)
                {
                    double dMomey = 0;
                    int nFirstIndex = 0;
                    int nSecondIndex = 0;
                    double dScale = new BisKeywords().GetScale(nKeywordsID);
                    if (dScale == 0)
                        dScale = 1;
                    switch (nUrlType)
                    {
                        //新闻源
                        case 1:
                            foreach (DataRow drWebSEO in dtWebSEO.Rows)
                            {
                                if (Convert.ToInt32(drWebSEO["FinalPageIndex"]) == 1)
                                    nFirstIndex++;
                                if (Convert.ToInt32(drWebSEO["FinalPageIndex"]) == 2)
                                    nSecondIndex++;
                            }
                            if (deductWeb != null)
                            {
                                if (nFirstIndex >= 2)
                                {
                                    dMomey = deductWeb.LinkFirstMomey.Value;
                                }
                                else
                                {
                                    if (nSecondIndex >= 2 || (nFirstIndex == 1 && nSecondIndex > 0))
                                        dMomey = deductWeb.LinkSecondMomey.Value;
                                }
                            }
                            else
                            {
                                if (nFirstIndex >= 2)
                                {
                                    dMomey = sysConfig.NewsFirstMomey.Value;
                                }
                                else
                                {
                                    if (nSecondIndex >= 2 || (nFirstIndex == 1 && nSecondIndex > 0))
                                        dMomey = sysConfig.NewsSecondMomey.Value;
                                }
                            }
                            break;
                        //外链
                        case 2:
                            foreach (DataRow drWebSEO in dtWebSEO.Rows)
                            {
                                if (Convert.ToInt32(drWebSEO["FinalPageIndex"]) == 1)
                                    nFirstIndex++;
                                if (Convert.ToInt32(drWebSEO["FinalPageIndex"]) == 2)
                                    nSecondIndex++;
                            }
                            if (deductWeb != null)
                            {
                                if (nFirstIndex >= 2)
                                {
                                    dMomey = deductWeb.LinkFirstMomey.Value;
                                }
                                else
                                {
                                    if (nSecondIndex >= 2 || (nFirstIndex == 1 && nSecondIndex > 0))
                                        dMomey = deductWeb.LinkSecondMomey.Value;
                                }
                            }
                            else
                            {
                                if (nFirstIndex >= 2)
                                {
                                    dMomey = sysConfig.LinkFirstMomey.Value;
                                }
                                else
                                {
                                    if (nSecondIndex >= 2 || (nFirstIndex == 1 && nSecondIndex > 0))
                                        dMomey = sysConfig.LinkSecondMomey.Value;
                                }
                            }
                            break;
                    }
                    if (dMomey > 0)
                    {
                        dtWebSEO.Dispose();
                        return dScale * dMomey;
                    }
                }
                dtWebSEO.Dispose();
            }
            return 0;
        }

        /// <summary>
        /// 计算文章收录报表
        /// </summary>
        /// <param name="nWebSEOID"></param>
        /// <param name="nKeywordsID"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <param name="nBaiduDay"></param>
        /// <returns></returns>
        public int CountArticleReport(int nWebSEOID, int nKeywordsID, DateTime dBeginTime, DateTime dEndTime, int nBaiduDay)
        {
            DataTable dtWebSEO = new BisWebSEO().GetWebSEOReportList(nWebSEOID, nKeywordsID, dBeginTime, dEndTime);
            if (dtWebSEO != null)
            {
                if (dtWebSEO.Rows.Count >= nBaiduDay)
                {
                    int nFirstIndex = 0;
                    foreach (DataRow drWebSEO in dtWebSEO.Rows)
                    {
                        if (Convert.ToInt32(drWebSEO["FinalPageIndex"]) == 1)
                            nFirstIndex++;
                    }
                    if (nFirstIndex >= 2)
                        return 1;
                    return 2;
                }
            }
            return 0;
        }

        /// <summary>
        /// 获取网编文章
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public ArticleSEO Get(int nID, int nAdminID)
        {
            SqlDataReader dr = SqlHelper.ExecuteReader(
                                                         SqlHelper.database,
                                                         CommandType.Text,
                                                         "select top 1 * from [ArticleSEO] where [ID]=@ID and (@AdminID=1 or [AdminID]=@AdminID)",
                                                         new SqlParameter("@ID", nID),
                                                         new SqlParameter("@AdminID", nAdminID)
                                                         );
            ArticleSEO Info = null;
            while (dr.Read())
            {
                Info = new ArticleSEO();
                Info.ID = Convert.ToInt32(dr["ID"]);
                if (dr["WebSEOID"] != DBNull.Value)
                    Info.WebSEOID = Convert.ToInt32(dr["WebSEOID"]);
                Info.SortID = Convert.ToInt32(dr["SortID"]);
                Info.AritcleTitle = dr["AritcleTitle"].ToString();
                Info.ArticleUrl = dr["ArticleUrl"].ToString();
                if (dr["KeywordsID"] != DBNull.Value)
                    Info.KeywordsID = Convert.ToInt32(dr["KeywordsID"]);
                Info.Keywords = dr["Keywords"].ToString();
                if (dr["KeywordsID1"] != DBNull.Value)
                    Info.KeywordsID1 = Convert.ToInt32(dr["KeywordsID1"]);
                Info.Keywords1 = dr["Keywords1"].ToString();
                Info.Status = Convert.ToBoolean(dr["Status"]);
                if (dr["IsPass"] != DBNull.Value)
                    Info.IsPass = Convert.ToBoolean(dr["IsPass"]);
                Info.UrlType = Convert.ToInt32(dr["UrlType"]);
                if (dr["AimID"] != DBNull.Value)
                    Info.AimID = Convert.ToInt32(dr["AimID"]);
                if (dr["IsDeduct"] != DBNull.Value)
                    Info.IsDeduct = Convert.ToBoolean(dr["IsDeduct"]);
                if (dr["DeductWebID"] != DBNull.Value)
                    Info.AimID = Convert.ToInt32(dr["DeductWebID"]);
                Info.Momey = Convert.ToDouble(dr["Momey"]);
                if (dr["AccountTime"] != DBNull.Value)
                    Info.AccountTime = Convert.ToDateTime(dr["AccountTime"]);
                Info.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
            }
            dr.Close();
            return Info;
        }

        /// <summary>
        /// 新增网编文章
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public int Insert(ArticleSEO Info)
        {
            string strSql = @"insert into [ArticleSEO]([AdminID],[SortID],[AritcleTitle],[ArticleUrl],[KeywordsID],[Keywords],[KeywordsID1],[Keywords1],[Status],[UrlType],[AimID]) 
                              values(@AdminID,@SortID,@AritcleTitle,@ArticleUrl,@KeywordsID,@Keywords,@KeywordsID1,@Keywords1,@Status,@UrlType,@AimID)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@AdminID", Info.AdminID));
            parms.Add(new SqlParameter("@SortID", Info.SortID));
            parms.Add(new SqlParameter("@AritcleTitle", Info.AritcleTitle));
            parms.Add(new SqlParameter("@ArticleUrl", Info.ArticleUrl));
            parms.Add(new SqlParameter("@KeywordsID", Info.KeywordsID));
            parms.Add(new SqlParameter("@Keywords", Info.Keywords));
            parms.Add(new SqlParameter("@KeywordsID1", Info.KeywordsID1));
            parms.Add(new SqlParameter("@Keywords1", Info.Keywords1));
            parms.Add(new SqlParameter("@Status", Info.Status));
            parms.Add(new SqlParameter("@UrlType", Info.UrlType));
            parms.Add(new SqlParameter("@AimID", Info.AimID));
            return SqlHelper.ExecuteQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray());
        }

        /// <summary>
        /// 更新网编文章
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="nAdminID"></param>
        /// <param name="strArticleUrl"></param>
        /// <returns></returns>
        public bool UpdateUrl(int nID, int nAdminID, int nSortID, string strArticleUrl)
        {
            string strSql = "update [ArticleSEO] set [ArticleUrl]=@ArticleUrl,[SortID]=@SortID where [ID]=@ID and  (@AdminID=1 or [AdminID]=@AdminID)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@SortID", nSortID));
            parms.Add(new SqlParameter("@ArticleUrl", strArticleUrl));
            parms.Add(new SqlParameter("@ID", nID));
            parms.Add(new SqlParameter("@AdminID", nAdminID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 验证网址是否已经存在
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="strArticleUrl"></param>
        /// <returns></returns>
        public bool CheckArticlesUrl(int nID, string strArticleUrl)
        {
            string strSql = "select top 1 [ID] from [ArticleSEO] where (@ID=0 or [ID]<>@ID) and [ArticleUrl]=@ArticleUrl";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ID", nID));
            parms.Add(new SqlParameter("@ArticleUrl", strArticleUrl));
            object retVal = SqlHelper.ExecuteScalar(
                            SqlHelper.database,
                            CommandType.Text,
                            strSql,
                            parms.ToArray());
            return retVal == null ? false : true;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="nAdminID"></param>
        /// <returns></returns>
        public bool Delete(int nID, int nAdminID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.Text,
                   "delete from [ArticleSEO] where [ID]=@ID and (@AdminID=1 or [AdminID]=@AdminID);",
                   new SqlParameter("@ID", nID),
                   new SqlParameter("@AdminID", nAdminID)) > 0;
        }


        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="bStatus"></param>
        /// <returns></returns>
        public bool UpdateStatus(int nID, int nWebSEOID, bool bStatus)
        {
            string strSql = "update [ArticleSEO] set [WebSEOID]=@WebSEOID,[Status]=@Status where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@WebSEOID", nWebSEOID));
            parms.Add(new SqlParameter("@Status", bStatus));
            parms.Add(new SqlParameter("@ID", nID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="bStatus"></param>
        /// <returns></returns>
        public bool UpdateStatus(int nID, bool bStatus)
        {
            string strSql = "update [ArticleSEO] set [Status]=@Status where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Status", bStatus));
            parms.Add(new SqlParameter("@ID", nID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 更新提成状态
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="bIsPass"></param>
        /// <param name="dMomey"></param>
        /// <returns></returns>
        public bool UpdateIsPass(int nID, bool bIsPass, double dMomey,DateTime dAccountTime)
        {
            string strSql = "update [ArticleSEO] set [IsPass]=@IsPass,[Momey]=@Momey,[AccountTime]=@AccountTime where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@IsPass", bIsPass));
            parms.Add(new SqlParameter("@Momey", dMomey));
            parms.Add(new SqlParameter("@AccountTime", dAccountTime));
            parms.Add(new SqlParameter("@ID", nID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        ///// <summary>
        ///// 结算
        ///// </summary>
        ///// <returns></returns>
        //public bool UpdateIsAccount()
        //{
        //    string strSql = "update [ArticleSEO] set [IsAccount]=1,[AccountTime]=getdate() where [IsAccount]=0";
        //    return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql) > 0;
        //}

        /// <summary>
        /// 更新特殊提成
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="bIsDeduct"></param>
        /// <param name="nDeductWebID"></param>
        /// <returns></returns>
        public bool UpdateDeduct(int nID, bool bIsDeduct, int nDeductWebID)
        {
            string strSql = "update [ArticleSEO] set [IsDeduct]=@IsDeduct,[DeductWebID]=@DeductWebID where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@IsDeduct", bIsDeduct));
            parms.Add(new SqlParameter("@DeductWebID", nDeductWebID));
            parms.Add(new SqlParameter("@ID", nID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="nAdminID"></param>
        /// <param name="strArticleUrl"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public DataTable GetList(int nAdminID, int nSortID, string strArticleUrl, string strKeywords, int nUrlType, DateTime dBeginTime, DateTime dEndTime, int nStatus, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[12];
            parms[0] = new SqlParameter("@AdminID", nAdminID);
            parms[1] = new SqlParameter("@SortID", nSortID);
            parms[2] = new SqlParameter("@ArticleUrl", strArticleUrl);
            parms[3] = new SqlParameter("@Keywords", strKeywords);
            parms[4] = new SqlParameter("@UrlType", nUrlType);
            parms[5] = new SqlParameter("@BeginTime", dBeginTime);
            parms[6] = new SqlParameter("@EndTime", dEndTime);
            parms[7] = new SqlParameter("@Status", nStatus);
            parms[8] = new SqlParameter("@PageIndex", nPageIndex);
            parms[9] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[10] = new SqlParameter("@PageCount", nPageCount);
            parms[10].Direction = ParameterDirection.Output;
            parms[11] = new SqlParameter("@RecordCount", nRecordCount);
            parms[11].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetArticleSEOList",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[10].Value);
            nRecordCount = Convert.ToInt32(parms[11].Value);
            return dt;
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="bStatus"></param>
        /// <returns></returns>
        public DataTable GetCrawlerArticleList(bool bStatus,DateTime dBeginTime, DateTime dEndTime)
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select [ID],[ArticleUrl],[SortID],[UrlType],[KeywordsID],[KeywordsID1],[CreateTime] from [ArticleSEO] where [Status]=@Status and [CreateTime] between @BeginTime and @EndTime",
                    new SqlParameter("@Status", bStatus),
                    new SqlParameter("@BeginTime", dBeginTime),
                    new SqlParameter("@EndTime", dEndTime)).Tables[0];
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="bStatus"></param>
        /// <returns></returns>
        public DataTable GetList(bool bStatus, DateTime dBeginTime,DateTime dEndTime)
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select [ID],[WebSEOID],[AritcleTitle],[UrlType],[SortID],[KeywordsID],[KeywordsID1],[ArticleUrl],[CreateTime] from [ArticleSEO] where [Status]=@Status and [CreateTime] between @BeginTime and @EndTime order by [ID]",
                    new SqlParameter("@Status", bStatus),
                    new SqlParameter("@BeginTime", dBeginTime),
                    new SqlParameter("@EndTime", dEndTime)).Tables[0];
        }

        /// <summary>
        /// 统计提成情况
        /// </summary>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public DataTable CountByMonth(DateTime dBeginTime, DateTime dEndTime)
        {
            string strSql = "select [AdminID],[UrlType],sum([Momey]) AllMomey from [ArticleSEO] where [IsPass]=1 and [CreateTime] between @BeginTime and @EndTime group by [AdminID],[UrlType] order by [AdminID]";
            return SqlHelper.ExecuteDataset(
                   SqlHelper.database,
                   CommandType.Text,
                   strSql,
                   new SqlParameter("@BeginTime", dBeginTime),
                   new SqlParameter("@EndTime", dEndTime)).Tables[0];
        }


        /// <summary>
        /// 获取关系列表
        /// </summary>
        /// <param name="nWebSEOID"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <param name="nPageIndex"></param>
        /// <param name="nPageSize"></param>
        /// <param name="nPageCount"></param>
        /// <param name="nRecordCount"></param>
        /// <returns></returns>
        public DataTable GetArticleSEORelation(int nWebSEOID, int nKeywordsID, int nKeywordsID1, DateTime dBeginTime, DateTime dEndTime, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[9];
            parms[0] = new SqlParameter("@WebSEOID", nWebSEOID);
            parms[1] = new SqlParameter("@KeywordsID", nKeywordsID);
            parms[2] = new SqlParameter("@KeywordsID1", nKeywordsID1);
            parms[3] = new SqlParameter("@BeginTime", dBeginTime);
            parms[4] = new SqlParameter("@EndTime", dEndTime);
            parms[5] = new SqlParameter("@PageIndex", nPageIndex);
            parms[6] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[7] = new SqlParameter("@PageCount", nPageCount);
            parms[7].Direction = ParameterDirection.Output;
            parms[8] = new SqlParameter("@RecordCount", nRecordCount);
            parms[8].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetArticleSEORelation",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[7].Value);
            nRecordCount = Convert.ToInt32(parms[8].Value);
            return dt;
        }

        /// <summary>
        /// 获取文章指定关键字统计
        /// </summary>
        /// <param name="strKeywordsIDList"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public DataTable GetKeywordsArticleList(string strKeywordsIDList,int nAdminID, bool bIsPass, DateTime dBeginTime, DateTime dEndTime)
        {
            string strSql = "select [KID],[UrlType],count([ID]) [AllCount] from [v_KeywordsArticle] where (@AdminID=0 or AdminID=@AdminID) and [CreateTime] between @BeginTime and @EndTime and [IsPass]=@IsPass and [KID] in(" + strKeywordsIDList + ") group by [KID],[UrlType]";
            List<SqlParameter> parms = new List<SqlParameter>(); 
            parms.Add(new SqlParameter("@AdminID", nAdminID));
            parms.Add(new SqlParameter("@BeginTime", dBeginTime));
            parms.Add(new SqlParameter("@EndTime", dEndTime));
            parms.Add(new SqlParameter("@IsPass", bIsPass));
            return SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.Text,
                strSql,
                parms.ToArray()
                ).Tables[0];
        }

        /// <summary>
        /// 获取文章指定关键字统计
        /// </summary>
        /// <param name="strKeywordsIDList"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public DataTable GetKeywordsArticleList(string strKeywordsIDList,int nAdminID,int nUrlType,int nBeginCount,int nEndCount, DateTime dBeginTime, DateTime dEndTime)
        {
            string strSql = "select [KID],count([ID]) [AllCount] from [v_KeywordsArticle] where [UrlType]=@UrlType and (@AdminID=0 or AdminID=@AdminID) and [CreateTime] between @BeginTime and @EndTime and [KID] in(" + strKeywordsIDList + ") group by [KID] having count([ID]) between @BeginCount and @EndCount";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@UrlType", nUrlType));
            parms.Add(new SqlParameter("@AdminID", nAdminID));
            parms.Add(new SqlParameter("@BeginTime", dBeginTime));
            parms.Add(new SqlParameter("@EndTime", dEndTime));
            parms.Add(new SqlParameter("@BeginCount", nBeginCount));
            parms.Add(new SqlParameter("@EndCount", nEndCount));
            return SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.Text,
                strSql,
                parms.ToArray()
                ).Tables[0];
        }

        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="nAdmin"></param>
        /// <param name="nUrlType"></param>
        /// <param name="nStatus"></param>
        /// <returns></returns>
        public int CountArticle(int nAdmin, int nUrlType, int nStatus, DateTime dBeginTime, DateTime dEndTime)
        {
            string strSql = "select count([ID]) from [ArticleSEO] where [AdminID]=@AdminID and [UrlType]=@UrlType and (@Status=-1 or [Status]=@Status) and [CreateTime] between @BeginTime and @EndTime";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@AdminID", nAdmin));
            parms.Add(new SqlParameter("@UrlType", nUrlType));
            parms.Add(new SqlParameter("@Status", nStatus));
            parms.Add(new SqlParameter("@BeginTime", dBeginTime));
            parms.Add(new SqlParameter("@EndTime", dEndTime));
            object retVal = SqlHelper.ExecuteScalar(
                                                   SqlHelper.database,
                                                   CommandType.Text,
                                                   strSql,
                                                   parms.ToArray());

            return retVal == DBNull.Value ? 0 : Convert.ToInt32(retVal);

        }

        /// <summary>
        /// 获取指定用户的文章信息
        /// </summary>
        /// <param name="nUserID"></param>
        /// <param name="nStatus"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public DataTable GetUserArticleList(int nUserID, bool bStatus, DateTime dBeginTime, DateTime dEndTime)
        {
            string strSql = "select [WebSEOID],[KeywordsID],[KeywordsID1],[Status],[UrlType] from [ArticleSEO] where [AdminID]=@AdminID and [Status]=@Status and [UrlType]<>3 and [CreateTime] between @BeginTime and @EndTime";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@AdminID", nUserID));
            parms.Add(new SqlParameter("@Status", bStatus));
            parms.Add(new SqlParameter("@BeginTime", dBeginTime));
            parms.Add(new SqlParameter("@EndTime", dEndTime));
            return SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.Text,
                strSql,
                parms.ToArray()
                ).Tables[0];
        }

        /// <summary>
        /// 获取抓情况
        /// </summary>
        /// <param name="strIDList"></param>
        /// <returns></returns>
        public DataTable GetArticleDayList(string strIDList)
        {
            return SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.Text,
                "select [KeysRelationID],[PageIndex],[RowIndex],[AllIndex],[DayIndex],[CreateTime] from [KeysRelationDay] where [KeysRelationID] in(" + strIDList + ") order by [KeysRelationID],[CreateTime]"
                ).Tables[0];
        }
    }
}
